package com.meihua.shellapp.activity.wallet;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okgo.model.Response;
import com.meihua.shellapp.R;
import com.meihua.shellapp.activity.AuthenticationInfoActivity;
import com.meihua.shellapp.base.BaseActivity;
import com.meihua.shellapp.base.MyApplication;
import com.meihua.shellapp.common.Constant;
import com.meihua.shellapp.common.NetHelper;
import com.meihua.shellapp.dialog.BaseMessageDialog;
import com.meihua.shellapp.utils.SharedPreferencesUtil;
import com.meihua.shellapp.utils.URL;
import com.meihua.shellapp.view.MyPwdEditText;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.OnClick;


/**
 * 添加银行卡验证密码
 * Created by Administrator on 2017/7/11.
 */

public class AddBankVerifyPwdActivity extends BaseActivity implements View.OnClickListener {
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.iv_top_left)
    ImageView mIVTopLeft;

    private MyPwdEditText add_bank_verify_pwd_et;//密码接收
    private TextView verify_pwd_tv_one;//密码显示框1
    private TextView verify_pwd_tv_two;//密码显示框2
    private TextView verify_pwd_tv_three;//密码显示框3
    private TextView verify_pwd_tv_four;//密码显示框4
    private TextView verify_pwd_tv_five;//密码显示框5
    private TextView verify_pwd_tv_six;//密码显示框6
    private BaseMessageDialog dialog;

    @Override
    public int getLayoutId() {
        return R.layout.activity_add_bank_verify_pwd;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        tvTitle.setVisibility(View.VISIBLE);
        mIVTopLeft.setVisibility(View.VISIBLE);
        tvTitle.setText("验证密码");
        verify_pwd_tv_one = (TextView) findViewById(R.id.verify_pwd_tv_one);
        verify_pwd_tv_two = (TextView) findViewById(R.id.verify_pwd_tv_two);
        verify_pwd_tv_three = (TextView) findViewById(R.id.verify_pwd_tv_three);
        verify_pwd_tv_four = (TextView) findViewById(R.id.verify_pwd_tv_four);
        verify_pwd_tv_five = (TextView) findViewById(R.id.verify_pwd_tv_five);
        verify_pwd_tv_six = (TextView) findViewById(R.id.verify_pwd_tv_six);
        add_bank_verify_pwd_et = (MyPwdEditText) findViewById(R.id.add_bank_verify_pwd_et);
        add_bank_verify_pwd_et.addTextChangedListener(textWatcher);
        /*判断是否设置过支付密码*/
        hasSetPayPassword();
    }


    private void hasSetPayPassword() {
        if (!SharedPreferencesUtil.getInstance().getString(this, Constant.PAY_PASSWARD, "").equals("1")) {
             /*去设置支付密码*/
            dialog = new BaseMessageDialog.Builder(this)
                    .setClickSure(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                           /*去设置支付密码*/
                            Intent intent = new Intent(AddBankVerifyPwdActivity.this, AuthenticationInfoActivity.class);
                            intent.putExtra("type", 5);
                            startActivity(intent);
                            dialog.dismiss();
                        }
                    }).setMessage("您还未设置支付密码~前往设置密码界面")
                    .show();
        }
    }


    /**
     * 文本监听事件
     */
    private TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            if (add_bank_verify_pwd_et.getText().toString().length() > 0) {
                verify_pwd_tv_one.setText("1");
            } else {
                verify_pwd_tv_one.setText("");
            }
            if (add_bank_verify_pwd_et.getText().toString().length() > 1) {
                verify_pwd_tv_two.setText("1");
            } else {
                verify_pwd_tv_two.setText("");
            }
            if (add_bank_verify_pwd_et.getText().toString().length() > 2) {
                verify_pwd_tv_three.setText("1");
            } else {
                verify_pwd_tv_three.setText("");
            }
            if (add_bank_verify_pwd_et.getText().toString().length() > 3) {
                verify_pwd_tv_four.setText("1");
            } else {
                verify_pwd_tv_four.setText("");
            }
            if (add_bank_verify_pwd_et.getText().toString().length() > 4) {
                verify_pwd_tv_five.setText("1");
            } else {
                verify_pwd_tv_five.setText("");
            }
            if (add_bank_verify_pwd_et.getText().toString().length() > 5) {
                verify_pwd_tv_six.setText("1");
                verifyPayPwd(add_bank_verify_pwd_et.getText().toString().trim());
            } else {
                verify_pwd_tv_six.setText("");
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    };

    /*密码输入完毕*/
    private void verifyPayPwd(final String pwd) {
        Map<String, String> params = new HashMap() {{
            put("pay_password", pwd);
            put("m_number", MyApplication.getInstance().getUserId());
        }};
        NetHelper.post(URL.getUrl().CHECKPAYMENT_INDEX, params, new NetHelper.HttpCallBack() {
            @Override
            public void onSuccess(Response<String> response) {
                JSONObject obj = null;
                try {
                    obj = new JSONObject(response.body());
                    if (obj.getInt("code") == 1) {
                        Toast.makeText(AddBankVerifyPwdActivity.this, "验证成功", Toast.LENGTH_SHORT).show();
                        Intent in = new Intent(AddBankVerifyPwdActivity.this, AddBankInfoActivity.class);
                        startActivity(in);
                        finish();
                    } else {
                        Toast.makeText(AddBankVerifyPwdActivity.this, "验证失败", Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, true);
    }

    @OnClick(R.id.iv_top_left)
    public void onViewClicked() {
        finish();
    }

}
